package com.wx;

import java.io.*;
import java.net.Socket;

public class WuxiaClient {
    public static final String userName = "wuxia";
    /**
     * 用于存放接收文件的文件夹路径
     */
    public static final String pathToStoreReceiveFile = "/Users/lijin/Downloads/" + userName;

    public static void main(String args[]) {

        try {
            //向本机的4700端口发出客户请求
            Socket socket = new Socket("127.0.0.1", 4700);
            /**
             * 负责接收消息
             */
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        InputStream inputStream = socket.getInputStream();
                        DataInputStream dis = new DataInputStream(inputStream);
                        try {
                            while (true) {
                                String msg = dis.readUTF();
                                System.out.println(msg);
                                if (msg.startsWith("$file$")) {
                                    System.out.println("即将传送文件");
                                    String fileSize = msg.split(",")[1];
                                    byte[] buf = new byte[Integer.valueOf(fileSize)];
                                    int len = 0;
                                    String fileName = msg.split(",")[2];
                                    String newFilePath = getClientFilePath(fileName);
                                    File file = new File(newFilePath);
                                    DataOutputStream dos = new DataOutputStream(
                                            new FileOutputStream(file));
                                    while ((len = dis.read(buf)) != -1) {
                                        dos.write(buf, 0, len);
                                        if (file.length() >= Long.valueOf(fileSize)) {
                                            break;
                                        }
                                    }
                                    dos.flush();
                                    dos.close();
                                    System.out.println("文件接收完毕:" + newFilePath);
                                }

                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
            }).start();

            BufferedReader sin = new BufferedReader(new InputStreamReader(System.in));
            OutputStream out;
            DataOutputStream os = new DataOutputStream(socket.getOutputStream());
            os.writeUTF("register," + userName);
            os.flush();
            while (true) {
                String s = sin.readLine();
                if (s.startsWith("msg")) {
                    s = s.replace("msg", "msg," + userName);
                    os.writeUTF(s);
                    os.flush();
                }
                //file,wuxia,TalkClient.java,/Users/lijin/Downloads/TalkClient.java
                if (s.startsWith("file")) {
                    //	String fileName=s.split(",")[2];
                    String filePath = s.split(",")[3];
                    s = s.replace("file", "file," + userName);
                    File file = new File(filePath);
                    os.writeUTF(s);
                    os.flush();
                    os.writeLong(file.length());
                    os.flush();
                    //传输文件
                    DataInputStream dis = new DataInputStream(new FileInputStream(filePath));
                    byte[] buf = new byte[Integer.valueOf(file.length() + "")];
                    int len = 0;
                    while ((len = dis.read(buf)) != -1) {
                        os.write(buf, 0, len);
                    }
                    os.flush();
                    dis.close();
                }

            }
        } catch (Exception e) {
            System.out.println("Error" + e); //出错，则打印出错信息
        }
    }

    private static String getClientFilePath(String fileName) {
        String path = pathToStoreReceiveFile + File.separator + fileName;
        File file = new File(path);
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        return path;
    }
}
